﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _17_Lambda表达式结合数组
{
    public class People
    {
        //对象数组集合
        public Person[] people = null;
        //people数组对象的元素个数量
        public int Count { get; set; } = 0;
        //中间变量用来存储临时数据
        private Person[] CacheArray = null;

        public void Add(Person person)
        {
            if (Count > 0)
            {
                //将之前的数组内容保存到中间数组当中，作为临时存储
                CacheArray = new Person[Count];
                for (int i = 0; i < Count; i++)
                {
                    CacheArray[i] = people[i];
                }
                //清空people数组的内容，并申请新的数组空间。数组空间为之前数组空间的长度+1
                Count++;//修改数组长度
                people = null;
                people = new Person[Count];
                //将中间数组当中的内容复制到people数组当中
                for (int i = 0; i < CacheArray.Length; i++)
                {
                    people[i] = CacheArray[i];
                }
            }
            else
            {
                //数组当中没有内容，数组长度+1
                Count++;
                //申请内存空间
                people = new Person[Count];
            }
            //对数组元素进行赋值
            people[Count - 1] = person;//对最后元素进行赋值
        }

        public void ForeEach(Func<Person, bool> func)
        {
            //遍历数组对象
            foreach (Person item in people)
            {
                if (func(item))
                {
                    item.Print();
                }
            }
        }

        public void ForEach(Action<Person> action)
        {
            foreach (Person item in people)
            {
                action(item);
            }
        }

        public People FindAll(Func<Person, bool> func)
        {
            //创建对象用于保存符合条件的元素
            People result = new People();
            //遍历添加集合
            foreach (Person item in people)
            {
                if (func(item))
                {
                    result.Add(item);
                }
            }
            //返回对象
            return result;
        }

        public bool GenderFunc(Person item)
        {
            return item.Gender == "女";
        }



        //public bool isGender(Person item)
        //{
        //    return item.Gender == "男";
        //}
    }
}
